/**
 * 
 */
package com.eis.performacetest.threadpool;

import java.io.Serializable;
import java.util.Date;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;

import com.eis.performacetest.util.PropertyReader;

/**
 * @author sh-panwei01
 *
 */
public class TestThread implements Runnable, Serializable {
	private static final long serialVersionUID = 6941646013099592118L;
	private static Logger logger = Logger.getLogger(TestThread.class);
	
	private String taskId = "";
	
	TestThread(String taskId) {
		this.taskId = taskId;
	}

	public void run() {
		String url = PropertyReader.getTCallurl();
		
		HttpClient httpClient = new HttpClient();
		GetMethod method = new GetMethod(url); 
		method.addRequestHeader("Accept", "application/xml");
		
		int repeatTime = PropertyReader.getTRepeatTime();
		for (int i = 0; i < repeatTime; i++) {
			int sleepTime = PropertyReader.getTRequestSleepTime();
			long startTimeMill = new Date().getTime();
			try {
				httpClient.executeMethod(method);
				System.out.print(".");
			} catch (Throwable e) {
				logger.error(taskId + ": error when http get: " + url, e);
			} finally {
				long endTimeMill = new Date().getTime();
				long responseTime = startTimeMill - endTimeMill;
				logger.info("[" + taskId + " - spentTime - " + responseTime + "] at round: " + i + " - ");
				
				try {
					Thread.sleep(sleepTime);
				} catch (InterruptedException e) {
					logger.error(taskId + ": error when sleep", e);
				}
			}
		}
		
		System.out.println(taskId + " is done!");
	}
	
	public Object getTask() {
		return this.taskId;
	}
}
